package controllers

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.didapinche.com/foundation/plat-echo/app/ops/services"
	promentheus "go.didapinche.com/foundation/plat-echo/pkg/prometheus"
	"go.uber.org/zap"
	"net/http"
)

type HookController struct {
	logger      *zap.Logger
	hookService services.HookService
}

func NewHookController(logger *zap.Logger, hookService services.HookService) *HookController {
	return &HookController{
		logger:      logger,
		hookService: hookService,
	}
}

func (h *HookController) Hook(c *gin.Context) {
	defer func() {
		i := recover()
		if i != nil {
			fmt.Println(i)
		}
	}()

	var info promentheus.AlertInfo
	err := c.BindJSON(&info)
	if err != nil {
		c.String(http.StatusBadRequest, "err=%v", err)
		return
	}
	// 启动一个协程
	go h.hookService.Dispatch(context.Background(), info)
	c.AbortWithStatus(http.StatusOK)
}
